package cn.edu.nchu.fragmentdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.osmdroid.views.MapView;

import cn.edu.nchu.fragmentdemo.fragments.UavRouteTaskFragment;
import cn.edu.nchu.fragmentdemo.utils.MapBasicOperationUtil;

/**
 * @author Tsuki
 */
public class MainActivity extends AppCompatActivity {

    private Button btnOpenPage;
    private MapView mMapView = null;
    private static MainActivity instance;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        init();
    }


    public void init() {
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        btnOpenPage = findViewById(R.id.btnOpenPage);
        final UavRouteTaskFragment uavRouteTaskFragment = new UavRouteTaskFragment();
        btnOpenPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addLeftFragment(uavRouteTaskFragment, uavRouteTaskFragment.getTAG());
                btnOpenPage.setVisibility(View.GONE);
            }
        });
        mMapView = findViewById(R.id.mapview);
        /* 地图初始化*/
        MapBasicOperationUtil mapBasicOperationUtil = new MapBasicOperationUtil(mMapView, getApplicationContext());
        mapBasicOperationUtil.initMap();
    }


    public static MainActivity getActivity() {
        return instance;
    }

    public MapView getMap() {
        return mMapView;
    }


    /**
     * 传入一个fragment用于替换右侧的占位视图
     *
     * @param fragment
     */
    public void addRightFragment(Fragment fragment, String Tag) {
        /**
         * 对于支持库中的Fragment使用
         */
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fragment_container_right, fragment, Tag);
        transaction.commit();
    }


    /**
     * 用于清除右侧的布局   （暂时为清除航线设置）
     */
    public void clearFragmentByTag(String Tag) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(Tag);
        //设置自定义退出动画   平移向左滑出
        transaction.setCustomAnimations(0, R.anim.slide_left_out);
        transaction.remove(fragment);
        transaction.commit();
    }


    /**
     * 传入一个fragment用于替换左侧的占位视图
     *
     * @param fragment
     */
    public void addLeftFragment(Fragment fragment, String Tag) {
        /**
         * 对于支持库中的Fragment使用
         */
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //设置自定义进入动画  平移向右滑入
        transaction.setCustomAnimations(R.anim.slide_left_in, 0);
        transaction.add(R.id.fragment_container_left, fragment, Tag);
        transaction.commit();


    }


    public void replaceRightFragment(Fragment fragment) {
        /**
         * 对于支持库中的Fragment使用
         */
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container_right, fragment);
        transaction.commit();
    }

}